package m4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();//连接
        Channel c = con.createChannel();//通信通道
        //创建 Direct 交换记
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);

        //向交换记发送消息   消息上需要携带路由键关键词
        while (true){
            System.out.println("输入消息：");
            String s = new Scanner(System.in).nextLine();
            System.out.println("输入路由建：");
            String k = new Scanner(System.in).nextLine();
            /*
            第一个参数  名字
            第二个参数  路由键  k
            第三个参数
            第四个参数  消息
             */
            c.basicPublish("direct_logs", k,null,s.getBytes(StandardCharsets.UTF_8));
        }
    }
}
